<?php
/**
 * 功能描述：此接口用于针对分类或物料号查找对应的促销活动
 *
 * 作者：zenan.li
 *
 * Created by Zend Studio.
 * User: zenan.li (FDG348)
 * Date: 2017/9/23
 * Time: 12:00
 */

namespace common\helps;

use common\models\ActivityMessage;
use common\models\GoodsSale;

class getActivityMessage{
    /**
     *  add by zenan.li (FDG348) 20170923
     */
    public function getActivityMessageData($sku,$category_id = []){
        $result = ['code'=>500,'data'=>'','message'=>''];
        try {
            if (empty($sku)) {
                throw new \Exception('物料号必传');
            }
            if (!is_array($sku)){
                throw new \Exception('物料号传参格式有误');
            }
            $today = date('Y-m-d');
            //通过物料号获取该物料号下对应的分类
            $return_data = $this->getCategoryId($sku);
            $sku_category_id = is_array($return_data) && count($return_data) > 0 ? array_column($return_data, 'genre_type') : [];
            $category_ids = is_array($category_id) && count($category_id) > 0 ? array_unique(array_merge($category_id,$sku_category_id)) : $sku_category_id;
            $activity_message_arr = [];
            if (count($category_ids) > 0 || count($sku) > 0){
                $normal_id = array_keys($GLOBALS['_GOODS_GENRE_TYPE']);
                $new_category_ids = [];
                foreach ($category_ids as $category_id){
                    if (!in_array($category_id, $normal_id)){
                        $new_category_ids[] = -1;
                    }else{
                        $new_category_ids[] = $category_id;
                    }
                }
                $new_category_ids[] = 0;
                $new_category_ids = array_unique($new_category_ids);
                $sql_where = 'j_activity_message.use_flg=1 and "'.$today .'" between j_activity_message.activity_start_date and j_activity_message.activity_end_date';
                if (count($new_category_ids) > 0){
                    $sql_where .= ' and (j_activity_message_plus.category_id in ('.implode(',', $new_category_ids).') || sku in ("'.implode('","', $sku).'"))';
                }else{
                    $sql_where .= ' and sku in ("'.implode('","', $sku).'")';
                }
                $select = 'j_activity_message.id,j_activity_message.activity_identification_style,j_activity_message.activity_identification,j_activity_message.activity_url,j_activity_message.title,j_activity_message.activity_start_date,j_activity_message.activity_end_date,j_activity_message_plus.category_id';
                $activity_message = ActivityMessage::find()->select($select)->joinWith('activityMessagePlus')->where($sql_where);
                $activity_message = $activity_message->asArray()->all();
                if (is_array($activity_message) && count($activity_message) > 0)foreach ($activity_message as $message){
                    //unset($message['id']);
                    unset($message['activityMessagePlus']);
                    $message['sku'] = in_array($message['category_id'], $sku_category_id) ? $return_data[$message['category_id']]['sku'] : '';
                    $activity_message_arr[] = $message;
                }
            }
            $result['code'] = 200;
            $result['data'] = $activity_message_arr;
            $result['message'] = '成功';
        } catch (\Exception $e) {
            $result['message'] = $e->getMessage();
        }
        return json_encode($result,JSON_UNESCAPED_UNICODE);
    }
    
    public function getCategoryId($sku){
        if (!is_array($sku) || count($sku) == 0){
            return false;
        }
        $category_id = GoodsSale::find()->select('sku,genre_type')->where(['in','sku',$sku])->asArray()->all();
        $category_id = array_combine(array_column($category_id, 'genre_type'), $category_id);
        if (tools::arrayHasValue($category_id,'')){
            unset($category_id['']);
        }
        return $category_id;
    }
}